﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace VS4CRM.Lib.Model
{
    public class XmlProjectHandler: IProjectHandler 
    {
        XmlDocument _projectXml;
        public XmlProjectHandler(XmlDocument projectXml)
        {
            _projectXml = projectXml;
        }

        public void AddFileToProject(string folder, string filename) 
        {
            AddFileToProject(folder, filename, String.Empty);
        }
        public void AddFileToProject(string folder, string filename, string[] dependands)
        {
            AddFileToProject(folder, filename, String.Empty);
            foreach (String file in dependands)
            {
                AddFileToProject(folder, file, filename);
            }
        }

        public void AddFileToProject(string folder, string filename, string parent)
        {
            XmlNamespaceManager ns = new XmlNamespaceManager(_projectXml.NameTable);
            ns.AddNamespace("def", "http://schemas.microsoft.com/developer/msbuild/2003");

            XmlNode fileNode = _projectXml.SelectSingleNode(
                String.Format("/def:Project/def:ItemGroup/def:Content[@Include=\"{1}\\{0}\"]", filename, folder), ns);
            if (fileNode == null)
            {
                fileNode = _projectXml.CreateElement("Content", "http://schemas.microsoft.com/developer/msbuild/2003");
                fileNode.Attributes.Append(XmlAttribute(_projectXml, "Include", String.Format("{1}\\{0}", filename, folder)));
                _projectXml.SelectSingleNode("/def:Project/def:ItemGroup", ns).AppendChild(fileNode);
            }
            if (!String.IsNullOrEmpty(parent))
            {
                XmlNode dependUpon = fileNode.SelectSingleNode("def:DependentUpon", ns);
                if (dependUpon == null)
                {
                    dependUpon = _projectXml.CreateElement("DependentUpon", "http://schemas.microsoft.com/developer/msbuild/2003");
                    fileNode.AppendChild(dependUpon);
                }
                dependUpon.InnerText = parent;
            }
        }

        private XmlAttribute XmlAttribute(XmlDocument xmlDoc, string attributeName,
                                          string attributeValue)
        {
            XmlAttribute xmlAttrib =
                xmlDoc.CreateAttribute(attributeName);
            xmlAttrib.Value = FilterXMLString(attributeValue);
            return xmlAttrib;
        }

        private string FilterXMLString(string inputString)
        {
            string returnString = inputString;
            if (inputString.IndexOf("&") > 0)
            {
                returnString = inputString.Replace("&", "&");
            }
            if (inputString.IndexOf("'") > 0)
            {
                returnString = inputString.Replace("'", "&apos;");
            }
            return returnString;
        }
    }
}
